$(function(){
	
	$("#user_password").blur(checkUserName) ;
	
	function checkUserName() {
		var password = $("#user_password").val() ;
		if (password.length < 6 || $.trim(password) =="") {
			$('#password').html("<span class='formtipErr onError'>密码不能为空，且至少6个字节</span>");
			return false;
		} else {
			$('#password').html("<span class='formtipSucc Success'></span>");
			return true;
		}
	}
	
	$("#repassword").blur(function(){
		var password = $("#user_password").val() ;
		var repassword = $("#repassword").val() ;
		if (repassword == null || repassword.length == "") {
			$('#passwords').html("<span class='formtipErr onError'>密码不能为空，且至少6个字节</span>");
		} else if (password == repassword) {
			$('#passwords').html("<span class='formtipSucc Success'></span>");
		} else {
			$('#passwords').html("<span class='formtipErr onError'>两次输入的密码不一致！</span>");
		}
	}) ;
	
	$("#user_userName").focus(function(){
		$("#username").html("<span id='name-hint' class='formtipSucc onWaiting'></span>");
	});
	
	$("#user_userName").blur(function(){
		var username = $("#user_userName").val() ;
		if (username.length < 4 || username.length > 20 || $.trim($("#user_userName").val()) == "") {
			$('#username').html("<span class='formtipErr onError'>用户名格式不正确,需在4~20个字节内</span>");
			return ;
		}
		$.post("AjaxAction!checkUserName",
				{"user.userName" : $("#user_userName").val()},
				function(data){
					if (data == "true") {
        				$('#username').html("<span class='formtipErr onError'>用户名已经存在！</span>");
        			} else if (data == "false") {
        				$('#username').html("<span class='formtipSucc onSuccess'></span>");
        			}
				}
		) ;
	}) ;
	
	$("#user_trueName").blur(function() {
		var trueName = $("#user_trueName").val() ;
		if (trueName.length < 2 || $.trim(trueName) =="") {
			$('#truename').html("<span class='formtipErr onError'>请填写您的真实姓名</span>");
		} else {
			$('#truename').html("<span class='formtipSucc Success'></span>");
		}
	}) ;
	
	$("#generateCode").click(function(){
		var timenow = new Date().getTime() ;
		$("#generateCode").attr('src', "AjaxAction!generateCode?d=" + timenow) ; 
	}) ;
	
	$("#inputCode").blur(function() {
		var code = $("#inputCode").val() ;
		if ($.trim($("#inputCode").val()) == "" || code.length != 4) {
			$('#validateCode').html("<span class='formtipsErr Error'>验证码不合法</span>");
			return false;
		}
		$.post("AjaxAction!checkValidateCode", 
				{"random" : $("#inputCode").val()},
				function(data) {
					if (data == "true") {
						$('#validateCode').html("<span class='formtipsSucc Success'></span>");
						return true;
        			} else if (data == "false") {
        				$('#validateCode').html("<span class='formtipsErr Error'>验证码错误</span>");
						return false;
        			}
				}
		) ;
		
	}) ;
	
	$("#user_tel").blur(function() {
		var tel = $("#user_tel").val() ;
		var regtel = /^[0-9\-\+\(\)]*$/ ;
		if ($.trim(tel) == "" || !regtel.test(tel) || tel.length < 11) {
			$('#tel').html("<span class='formtipErr Error'>手机号码格式不正确</span>");
		} else {
			$('#tel').html("<span class='formtipsSucc Success'></span>") ;
		}
	}) ;
	
	$("#user_email").focus(function(){
		$("#email").html("<span id='name-hint' class='formtipSucc onWaiting'></span>");
	});
	
	$("#user_idCard").blur(function() {
		var idCard = $("#user_idCard").val() ;
		var regIdCard = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
		if ($.trim(idCard) == "" || !regIdCard.test(idCard) || idCard.length != 18) {
			$('#idCard').html("<span class='formtipErr Error'>身份证格式不正确</span>");
		} else {
			$('#idCard').html("<span class='formtipsSucc Success'></span>") ;
		}
	}) ;
	
	$("#user_email").blur(function() {
		var useremail = $("#user_email").val() ;
		var regemail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
		if ($.trim(useremail) == "" || !regemail.test(useremail)) {
			$('#email').html("<span class='formtipErr Error'>邮箱格式不正确</span>");
			return false;
		}
		$.post("AjaxAction!checkEamil", 
				{"user.email" : $("#user_email").val()},
				function(data) {
					if (data == "true") {
						$('#email').html("<span class='formtipErr Error'>邮箱已被注册</span>");
        			} else if (data == "false") {
        				$('#email').html("<span class='formtipSucc Success'></span>");
        			}
				}
		) ;
	}) ;
	
}) ;
